package org.authsum.stitches.wicket.attribute.detail;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.authsum.stitches.wicket.contentdef.*;
import org.authsum.stitches.wicket.contentdef.detail.ContentDefPanel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.authsum.stitches.wicket.BasePanel;
import org.authsum.stitches.external.*;
import org.authsum.stitches.services.StitchesService;
import org.authsum.stitches.wicket.DetailPanel;


public class AttributePanel  extends DetailPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Map<String, String> CHOICES_MAP = new HashMap<String, String>();
	static {
		CHOICES_MAP.put("String", "String");
		CHOICES_MAP.put("Integer", "Integer");
		CHOICES_MAP.put("Boolean", "Boolean");
		CHOICES_MAP.put("Binary", "Binary");
		
	}

	public static DropDownChoice getValueTypeDropDownChoice() {
		
		Model dropDownModel = new Model() {
			public Serializable getObject() {
				return new ArrayList(CHOICES_MAP.keySet());
			}
		};
		
		DropDownChoice ddc = new DropDownChoice("valueType",
				dropDownModel, new IChoiceRenderer() {

					public String getDisplayValue(Object object) {
						return CHOICES_MAP.get(object);
					}

					public String getIdValue(Object object, int index) {
						return object.toString();
					}
				});
		return ddc;
	}
	private ContentDefHolder contentDefHolder = null;
	private ModalWindow attributeModal = null;
	private AttributeHolder attributeHolder = null;
	private ContentDefPanel contentDefPanel = null;
	private ContentDefsPanel contentDefsPanel = null;
	public AttributePanel(String id,ContentDefHolder contentDefHolder,	ModalWindow attributeModal,ContentDefPanel contentDefPanel,ContentDefsPanel contentDefsPanel) {
		super(id);
		this.setOutputMarkupId(true);
		this.attributeModal = attributeModal;
		this.contentDefHolder = contentDefHolder;
		this.attributeHolder = new AttributeHolder();
		this.contentDefPanel  = contentDefPanel;
		this.contentDefsPanel = contentDefsPanel;
		this.form = new Form("attributeForm", new CompoundPropertyModel(attributeHolder));
		add(form);
		addSaveButtons();
		form.add(newRequiredTextField("name", 255));
		//form.add(newRequiredTextField("displayName", 255));
		

		
		
		TextArea contentDefDescription = new TextArea("description");
		form.add(contentDefDescription);
		
		form.add(new TextField("mimeType"));
		
		form.add(getValueTypeDropDownChoice());
		form.add(new CheckBox("publishToAmazonS3"));
		form.add(new CheckBox("required"));


		this.postInit();
		
		
	}

	@Override
	protected List<String> getValidity() {
		List list = new ArrayList();
		check("mimeType",attributeHolder.getMimeType(),100,list,false);
		check("name",attributeHolder.getName(),100,list,true);
		check("Description",attributeHolder.getDescription(),100,list,false);
		return list;
	}

	@Override
	protected void saveDetailObject(AjaxRequestTarget target) {
		ContentDefRequest contentDefRequest = new ContentDefRequest();
		contentDefRequest.setAttributeHolder(attributeHolder);
		contentDefRequest.setContentDefId(contentDefHolder.getId());
		
		ContentDefResponse contentDefResponse = getStitchesService().addAttribute(contentDefRequest);
		//need to close diaglog
		//we should probably call back to contentdefpanel
		attributeModal.close(target);
		contentDefsPanel.editContentDef(target,contentDefHolder);
		//target.addComponent(contentDefPanel);
		
		
		
	}

}
